package com.frinder.configurer;

import com.frinder.base.Result;
import com.frinder.cglib.AbstractExtraPropertyAdapter;
import com.frinder.cglib.ExtraProperties;
import com.frinder.cglib.ExtraProperty;
import com.frinder.model.User;
import com.github.pagehelper.PageInfo;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by frinder on 2017/10/16.
 */
@Primary
@Component
public class MyExtraPropertyAdapter extends AbstractExtraPropertyAdapter {

    @Override
    public Object getResult(ExtraProperties extraProperties, Object dest) {
        if (dest instanceof User) {
            dest = advisorTemplate.getTarget4MultipleProperty(extraProperties, dest);
        } else if (dest instanceof List) {
            dest = advisorTemplate.getTargetList4MultipleProperty(extraProperties, (List<Object>) dest);
        } else if (dest instanceof Result) {
            Result result = (Result) dest;
            Object target = result.getData();
            if (target instanceof User) {
                target = advisorTemplate.getTarget4MultipleProperty(extraProperties, target);
            } else if (target instanceof PageInfo) {
                PageInfo pageInfo = (PageInfo) target;
                List<Object> list = advisorTemplate.getTargetList4MultipleProperty(extraProperties, pageInfo.getList());
                pageInfo.setList(list);
                target = pageInfo;
            }
            result.setData(target);
        }
        return dest;
    }

    @Override
    public Object getResult(ExtraProperty extraProperty, Object dest) {
        if (dest instanceof User) {
            dest = advisorTemplate.getTarget(extraProperty, dest);
        } else if (dest instanceof List) {
            dest = advisorTemplate.getTargetList(extraProperty, (List<Object>) dest);
        } else if (dest instanceof Result) {
            Result result = (Result) dest;
            Object target = result.getData();
            if (target instanceof User) {
                target = advisorTemplate.getTarget(extraProperty, target);
            } else if (target instanceof PageInfo) {
                PageInfo pageInfo = (PageInfo) target;
                List<Object> list = advisorTemplate.getTargetList(extraProperty, pageInfo.getList());
                pageInfo.setList(list);
                target = pageInfo;
            }
            result.setData(target);
        }
        return dest;
    }
}
